using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Utils;

public class PlayerPanel : MonoBehaviour
{
    private Player player;
    private GameObject rightTopGO;
    private GameObject leftTopGO;
    
    private Slider hpSlider;
    private Slider mpSlider;
    private Slider armSlider;
    private TextMeshProUGUI hpText;
    private TextMeshProUGUI mpText;
    private TextMeshProUGUI armText;

    private TextMeshProUGUI coinText;

    private Button pauseBtn;
    private void Awake()
    {
        player = GameController.Instance().playerCtr.Player;
        
        rightTopGO = UITool.GetChildGameObject("RightTop", gameObject);
        leftTopGO = UITool.GetChildGameObject("Info", gameObject);

        hpSlider = UITool.GetComponent<Slider>("HP", leftTopGO);
        mpSlider = UITool.GetComponent<Slider>("MP", leftTopGO);
        armSlider = UITool.GetComponent<Slider>("Arm", leftTopGO);
        hpText = UITool.GetComponent<TextMeshProUGUI>("HPText", hpSlider.gameObject);
        mpText = UITool.GetComponent<TextMeshProUGUI>("MPText", mpSlider.gameObject);
        armText = UITool.GetComponent<TextMeshProUGUI>("ArmText", armSlider.gameObject);

        coinText = UITool.GetComponent<TextMeshProUGUI>("CoinInfo/CoinText", rightTopGO);

        pauseBtn = UITool.GetComponent<Button>("Btns/PauseBtn", rightTopGO);
    }

    private void Start()
    {
        ResetAllValue();
    }

    private void OnEnable()
    {
        AddButtonClickEvent();
    }

    private void Update()
    {
        UpdateAllValue();//
    }

    private void OnDestroy()
    {
        pauseBtn.onClick.RemoveListener(PauseGame);
    }

    private void AddButtonClickEvent()
    {
        pauseBtn.onClick.AddListener(PauseGame);
    }

    private void PauseGame()
    {
        GameController.Instance().panelCtr.LoadPausePanel();
    }

    public void UpdateAllValue()
    { 
        hpSlider.value = player.hp;
        mpSlider.value = player.mp;
        armSlider.value = player.arm;
        hpText.text = hpSlider.value + " / " + hpSlider.maxValue;
        mpText.text = mpSlider.value + " / " + mpSlider.maxValue;
        armText.text = armSlider.value + " / " + armSlider.maxValue;
        
        coinText.text = GameController.Instance().archiveCtr.TempLevelData.goldNum.ToString();
    }

    private void ResetAllValue()
    { 
        hpSlider.value = player.hp;
        hpSlider.maxValue = player.maxHp;
        mpSlider.value = player.mp;
        mpSlider.maxValue = player.maxMp;
        armSlider.value = player.arm;
        armSlider.maxValue = player.maxArm;

        hpText.text = hpSlider.value + " / " + hpSlider.maxValue;
        mpText.text = mpSlider.value + " / " + mpSlider.maxValue;
        armText.text = armSlider.value + " / " + armSlider.maxValue;

        coinText.text = GameController.Instance().archiveCtr.TempLevelData.goldNum.ToString();
    }

    public void SetMPValue(float deltaValue)
    {
        player.mp += deltaValue;
    }
}
